﻿using Newtonsoft.Json.Linq;

namespace CommonUtils
{
    public class HostSettings
    {
        static string settingPath = "settings-" + ApplicationUtil.HostName + ".json";

        public static JObject settings;

        public static void Save()
        {
            FileUtil.Save(settingPath, settings.ToString());
        }

        static HostSettings()
        {
            settings = JsonUtil.GetJObjectFromFile(settingPath);
            if (settings == null)
            {
                settings = new JObject();
                settings.Add("@key", "@value");
                Save();
            }
        }

        public static T Get<T>(string key, T defaultValue = default)
        {
            if (!settings.ContainsKey(key))
            {
                settings.Add(new JProperty(key, defaultValue));
                Save();
            }
            return settings.Get<T>(key);
        }
    }
}
